package com.qf.controller;

import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.qf.feign.GoodsFeign;
import com.qf.pojo.Goods;
import com.qf.sevice.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * restTemplate:实现跨服务调用的对象
 * @version v1.0.0
 * @Description:
 * @author: 姜红凯
 * @date: 2022.03.14
 * @since 1.0
 */
@RestController
public class OrderController {

    @Autowired
    TestService testService;
    //自动注入RestTemplate对象
    @Autowired
    RestTemplate restTemplate;

    //@Autowired
    //EurekaClient eurekaClient;

    @RequestMapping("/testCache")
    public void testCache(){
        System.out.println(testService.test(1));
        System.out.println(testService.test(1));
        System.out.println(testService.test(1));
        System.out.println(testService.test(1));
        testService.clearCache(1);
        System.out.println(testService.test(1));
    }

    @Autowired
    GoodsFeign goodsFeign;

    @RequestMapping("/query/{i}")
    @HystrixCommand(fallbackMethod = "fallback")
    public String query(@PathVariable Integer i){
//
//        //获取 qf-goods的实例对象
//        //根据Eureka上注册的服务名称获取实例对象
//        InstanceInfo instanceInfo = eurekaClient.getNextServerFromEureka("qf-goods", false);
//
//        String url = instanceInfo.getHomePageUrl();
//        System.out.println(url);
//        //调用商品模块
//        //响应实体对象
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity(url +"query", String.class);
//        ResponseEntity<String> responseEntity = restTemplate.getForEntity("http://QF-GOODS/query", String.class);
//        String body = responseEntity.getBody();
        String body = goodsFeign.query(i);
//        String body = goodsFeign.findDetails(16);
//        Goods goods = new Goods();
//        goods.setId(1);
//        goods.setName("赵英杰");
//        goods.setPrice(9999.9);
//        String body = goodsFeign.save(goods);
        return "查询订单 ------" + body;
    }

    public String fallback(Integer i){
        System.out.println("服务降级了");
        return "服务降级了";
    }

}